package studyguide.ch09.graphics;

import java.io.IOException;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class GraphicsMidlet extends MIDlet {

	private final Command exitCommand;
	private final Command nextCommand;
	private final Display display;
	
	private final GraphicsCanvasA canvasA;
	private final GraphicsCanvasB canvasB;
	private final GraphicsCanvasC canvasC;
	private final GraphicsCanvasD canvasD;
	private final GraphicsCanvasE canvasE;
	private final GraphicsCanvasF canvasF;
	private final GraphicsCanvasG canvasG;
	private final GraphicsCanvasH canvasH;
	private final GraphicsCanvasI canvasI;
	
	private class SampleCommandListener implements CommandListener {

		public void commandAction(Command command, Displayable displayable) {
			if (command == exitCommand) {
				destroyApp(true);
				notifyDestroyed();
			} else if (command == nextCommand) {
				if (displayable == canvasA) {
					display.setCurrent(canvasB);
				} else if (displayable == canvasB) {
					display.setCurrent(canvasC);
				} else if (displayable == canvasC) {
					display.setCurrent(canvasD);
				} else if (displayable == canvasD) {
					display.setCurrent(canvasE);
				} else if (displayable == canvasE) {
					display.setCurrent(canvasF);
				} else if (displayable == canvasF) {
					display.setCurrent(canvasG);
				} else if (displayable == canvasG) {
					display.setCurrent(canvasH);
				} else if (displayable == canvasH) {
					display.setCurrent(canvasI);
				} else if (displayable == canvasI) {
					display.setCurrent(canvasA);
				}
			} else {
				throw new RuntimeException("UNKNOWN COMMAND" + command);
			}
		}
	}
	
	public GraphicsMidlet() throws IOException {
		canvasA = new GraphicsCanvasA();
		canvasB = new GraphicsCanvasB();
		canvasC = new GraphicsCanvasC();
		canvasD = new GraphicsCanvasD();
		canvasE = new GraphicsCanvasE();
		canvasF = new GraphicsCanvasF();
		canvasG = new GraphicsCanvasG();
		canvasH = new GraphicsCanvasH();
		canvasI = new GraphicsCanvasI();
		
		exitCommand = new Command("Exit", Command.EXIT, 1);
		nextCommand = new Command("Next", Command.OK, 1);
		
		canvasA.addCommand(exitCommand);
		canvasA.addCommand(nextCommand);
		canvasB.addCommand(exitCommand);
		canvasB.addCommand(nextCommand);
		canvasC.addCommand(exitCommand);
		canvasC.addCommand(nextCommand);
		canvasD.addCommand(exitCommand);
		canvasD.addCommand(nextCommand);
		canvasE.addCommand(exitCommand);
		canvasE.addCommand(nextCommand);
		canvasF.addCommand(exitCommand);
		canvasF.addCommand(nextCommand);
		canvasG.addCommand(exitCommand);
		canvasG.addCommand(nextCommand);
		canvasH.addCommand(exitCommand);
		canvasH.addCommand(nextCommand);
		canvasI.addCommand(exitCommand);
		canvasI.addCommand(nextCommand);
		
		SampleCommandListener listener = new SampleCommandListener();
		canvasA.setCommandListener(listener);
		canvasB.setCommandListener(listener);
		canvasC.setCommandListener(listener);
		canvasD.setCommandListener(listener);
		canvasE.setCommandListener(listener);
		canvasF.setCommandListener(listener);
		canvasG.setCommandListener(listener);
		canvasH.setCommandListener(listener);
		canvasI.setCommandListener(listener);
		
		display = Display.getDisplay(this);
	}
	
	protected void destroyApp(boolean unconditional) {
		// No Op
	}

	protected void pauseApp() {
		// No Op
	}

	protected void startApp() throws MIDletStateChangeException {
		display.setCurrent(canvasA);
	}

}
